/**
 * Copyright (C) 2010 Daniel <daniel.petisme@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * 
 */
package fr.daniel.todolist.model.pojo;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 * @author daniel
 *
 */

/**
 * Represent a task to do with a level of priority
 */

@SuppressWarnings("serial")
@Entity
@Table(name = "TODO")
@SequenceGenerator(name="seq", initialValue=10, allocationSize=100)
public class Todo implements Serializable {

	private static Logger log = Logger.getLogger(Todo.class);

	@Id
	@Column(name = "TODO_ID", nullable = false)
	@GeneratedValue(strategy = GenerationType.SEQUENCE)
	private int id;

	/** The name of the task todo */
	@Column(name = "TODO_LABEL", length = 50, nullable = false)
	private String label;

	/** The priority of the task by default is lowest */
	@ManyToOne(targetEntity = Priority.class, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	@JoinColumn(name = "PRIORITY_ID", referencedColumnName = "PRIORITY_ID")
	private Priority priority = Priority.LOWEST;

	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the label
	 */
	public String getLabel() {
		return label;
	}

	/**
	 * @param label
	 *            the label to set
	 */
	public void setLabel(String label) {
		this.label = StringUtils.trim(label);
	}

	/**
	 * @return the priority
	 */
	public Priority getPriority() {
		return priority;
	}

	/**
	 * @param priority
	 *            the priority to set
	 */
	public void setPriority(Priority priority) throws NullPointerException {
		this.priority = priority;
	}

	/**
	 * Default Constructor
	 */
	public Todo() {
		setLabel("Default Label");
		setPriority(Priority.LOWEST);
		log.debug(new StringBuilder("New : ").append(this).toString());
	}

	/**
	 * @param label
	 *            The name of the task to do
	 * @param priority
	 *            The priority of the thing to do
	 */
	public Todo(String label, Priority priority) {
		setLabel(label);
		setPriority(priority);
		log.debug(new StringBuilder("New : ").append(this).toString());
	}

	/**
	 * By default the priority is LOWEST
	 * 
	 * @param label
	 *            The name of the task to do
	 */
	public Todo(String label) {
		this(label, Priority.LOWEST);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		int prime = 31;
		int result = 1;
		result = prime * result + ((label == null) ? 0 : label.hashCode());
		result = prime * result
				+ ((priority == null) ? 0 : priority.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {

		boolean response = false;
		if (this == obj) {
			return true;
		} else {
			if (obj != null) {
				if (obj instanceof Todo) {
					Todo other = (Todo) obj;
					if (other.getLabel() != null && other.getPriority() != null) {
						if (label.equals(other.getLabel())
								&& priority.equals(other.getPriority())) {
							if (hashCode() == other.hashCode()) {
								response = true;
							}
						}
					}
				}
			}
		}
		return response;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Todo [");
		if (label != null) {
			builder.append("label=");
			builder.append(label);
			builder.append(", ");
		}
		if (priority != null) {
			builder.append("priority=");
			builder.append(priority);
		}
		builder.append("]");
		return builder.toString();
	}

}
